{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 特征值的实现",
   "id": "81d604620703e70e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### （1）np.linalg.eig() 获得方阵的特征值和特征向量",
   "id": "996c1d48c79a2f72"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 1,
   "source": [
    "import numpy as np\n",
    "B = np.array([[4,2],[1,5]])\n",
    "A = np.array(B)"
   ],
   "id": "initial_id"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:21:21.025892Z",
     "start_time": "2025-11-02T12:21:20.931064Z"
    }
   },
   "cell_type": "code",
   "source": "eig_val, eig_vec = np.linalg.eig(A)",
   "id": "a5b3b03be7ced568",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:21:24.974470Z",
     "start_time": "2025-11-02T12:21:24.958328Z"
    }
   },
   "cell_type": "code",
   "source": "eig_val",
   "id": "d59ea875d3cc14f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 6.])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:21:37.314633Z",
     "start_time": "2025-11-02T12:21:37.301228Z"
    }
   },
   "cell_type": "code",
   "source": "eig_vec",
   "id": "b97af7ad6b4e00a3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.89442719, -0.70710678],\n",
       "       [ 0.4472136 , -0.70710678]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:24:04.168352Z",
     "start_time": "2025-11-02T12:24:04.150144Z"
    }
   },
   "cell_type": "code",
   "source": "eig_vec.T.dot(eig_vec)",
   "id": "3e74e549c568be63",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.31622777],\n",
       "       [0.31622777, 1.        ]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### （2）验证Axeig_vex = eig_valxeig_vex",
   "id": "6cbf1b080c055494"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:23:13.125745Z",
     "start_time": "2025-11-02T12:23:13.111449Z"
    }
   },
   "cell_type": "code",
   "source": [
    "C1 = eig_val*eig_vec\n",
    "C2 = A.dot(eig_vec)"
   ],
   "id": "dfe69c7befc694db",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:23:44.273623Z",
     "start_time": "2025-11-02T12:23:44.257134Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1 = C2???: \\n\", np.allclose(C1, C2))",
   "id": "d04a92640a00f050",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1 = C2???: \n",
      " True\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:23:56.493853Z",
     "start_time": "2025-11-02T12:23:56.488346Z"
    }
   },
   "cell_type": "code",
   "source": "C2",
   "id": "fe5106f44bb72e7e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.68328157, -4.24264069],\n",
       "       [ 1.34164079, -4.24264069]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:24:36.094427Z",
     "start_time": "2025-11-02T12:24:36.087882Z"
    }
   },
   "cell_type": "code",
   "source": "eig_vec.T*eig_vec",
   "id": "ac641580f21ad448",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.8       , -0.31622777],\n",
       "       [-0.31622777,  0.5       ]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:32:32.605617Z",
     "start_time": "2025-11-02T12:32:32.589078Z"
    }
   },
   "cell_type": "code",
   "source": "eig_vec",
   "id": "44540ac5bb23213b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.89442719, -0.70710678],\n",
       "       [ 0.4472136 , -0.70710678]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T12:34:19.645700Z",
     "start_time": "2025-11-02T12:34:19.628126Z"
    }
   },
   "cell_type": "code",
   "source": "eig_vec[0][0]*eig_vec[1][1]+eig_vec[1][0]*eig_vec[0][1]",
   "id": "a221a66a9c892256",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.316227766016838"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### （3）根据特征值生成特征值矩阵sigma,验证Axeig_vex = eig_vecxsigma 但是Axeig_vex不等于sigmaxeig_vex",
   "id": "39db21dbc532dc14"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T22:56:06.906023Z",
     "start_time": "2025-11-02T22:56:06.896513Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sigma = np.diag(eig_val)\n",
    "print(\"验证Axeig_vex与eig_vecxsigma是否相等：\\n\",np.allclose(A.dot(eig_vec),eig_vec.dot(sigma)))"
   ],
   "id": "1ef87281a5ccf43d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证Axeig_vex与eig_vecxsigma是否相等：\n",
      " True\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T22:56:49.214847Z",
     "start_time": "2025-11-02T22:56:49.203771Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"验证Axeig_vex与sigmaxeig_vex是否相等，\\n\",np.allclose(A.dot(eig_vec),sigma.dot(eig_vec)))",
   "id": "bc0a637f417476c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证Axeig_vex与sigmaxeig_vex是否相等，\n",
      " False\n"
     ]
    }
   ],
   "execution_count": 24
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
